package com.fox0g.service.impl;

import com.fox0g.mapper.FarmMapper;
import com.fox0g.mapper.FreshfoodMapper;
import com.fox0g.netty.NettyServerHandler;
import com.fox0g.pojo.Sharebike;
import com.fox0g.service.FarmService;
import com.fox0g.service.SharebikeService;
import com.fox0g.mapper.SharebikeMapper;
import com.fox0g.websocket.WebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;

/**
* @author 86176
* @description 针对表【sharebike(共享单车)】的数据库操作Service实现
* @createDate 2024-07-05 21:32:23
*/
@Service
public class SharebikeServiceImpl implements SharebikeService{
    ArrayList<String> list = new ArrayList<>();
    //    @Autowired
//    ShengxiangService shengxiangService;
    @Autowired
    FarmService farmService;

    @Autowired
    FarmMapper farmMapper;

    @Autowired
    WebSocketHandler webSocketHandler;
    @Autowired
    NettyServerHandler nettyServerHandler;
    @Autowired
    FreshfoodMapper freshfoodMapper;

    @Autowired
    SharebikeMapper sharebikeMapper;


    public static String Meg = "";

    @Override
    public void HandleMessage(String message) throws IOException {


        webSocketHandler.send(message);
//        解析温湿度光照强度数据帧
        //HWV122.0I12TS6T123.3T221.0Z
        if (message.matches("HWV1[0-9]+.[0-9]I[0-9]+TS[0-9]+T1[0-9]+.[0-9]T2[0-9]+Z")) {

            String arrnews1[] = message.split("V1");
            String arrnew2[] = arrnews1[1].split("I");
            String arrnew3[] = arrnew2[1].split("TS");
            String arrnew4[] = arrnew3[1].split("T1");
            String arrnew5[] = arrnew4[1].split("T2");
            String arrnew6[] = arrnew5[1].split("Z");
            String V1 = arrnew2[0];  //截取电池电压
            String I = arrnew3[0];  //放电电流
            String TS = arrnew4[0]; //充电温差
            String T1 = arrnew5[0]; //温度1
            String T2 = arrnew6[0]; //温度2
            String now = LocalDateTime.now().toString(); //获取当前时间
            //向前端发送这些数据帧
            webSocketHandler.send("t" + arrnew2[0]);
            webSocketHandler.send("Hd" + arrnew3[0]);
            webSocketHandler.send("ls" + arrnew4[0]);
            //将解析后的数据设置给pojo类
            Sharebike sharebike = new Sharebike();
            sharebike.setBikeid("202133");
            sharebike.setCreateTime(now);
            sharebike.setSubt(TS);
            sharebike.setV1(V1);
            sharebike.setI1(I);
            int newid = sharebikeMapper.selectid();
            if (sharebikeMapper.selectrfid(newid) != null) {
                sharebike.setBatteryrfid(sharebikeMapper.selectrfid(newid));
                sharebikeMapper.insert(sharebike);
                System.out.println("插入成功！");
            }




        }

        String cardmatch = "HWPID[0-9A-Z]+Z";
        if (message.matches("HWPID[0-9A-Z]+Z")) {
            System.out.println("符合");
            String card = message.substring(5, message.length() - 1);
            int newid1 = sharebikeMapper.selectid();
            Sharebike selectsharebike = sharebikeMapper.selectformation(newid1);
            selectsharebike.setBatteryrfid(card);
            sharebikeMapper.insert(selectsharebike);
            System.out.println("更换电池成功！");
        }



    }

}




